package cn.bingoo.handy;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import cn.bingoo.leaksuspects.LeakSuspects;
import cn.bingoo.swing.utils.Swings;

public class SqlLeakSuspectsFrame extends JFrame {

    private static final long serialVersionUID = 3157223409175104208L;
    private JPanel contentPane;
    private JTextField textScanPath;
    private JTextField txtCount;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    SqlLeakSuspectsFrame frame = new SqlLeakSuspectsFrame();
                    frame.setVisible(true);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public SqlLeakSuspectsFrame() {
        setTitle("SqlLeakSuspects");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 730, 458);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[] { 0, 0 };
        gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0 };
        gbl_contentPane.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
        gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, 1.0, Double.MIN_VALUE };
        contentPane.setLayout(gbl_contentPane);

        JPanel panel = new JPanel();
        GridBagConstraints gbc_panel = new GridBagConstraints();
        gbc_panel.insets = new Insets(0, 0, 5, 0);
        gbc_panel.fill = GridBagConstraints.BOTH;
        gbc_panel.gridx = 0;
        gbc_panel.gridy = 0;
        contentPane.add(panel, gbc_panel);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[] { 0, 0, 0, 0, 0, 0 };
        gbl_panel.rowHeights = new int[] { 0, 0 };
        gbl_panel.columnWeights = new double[] { 1.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
        gbl_panel.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
        panel.setLayout(gbl_panel);

        textScanPath = new JTextField();
        GridBagConstraints gbc_textScanPath = new GridBagConstraints();
        gbc_textScanPath.insets = new Insets(0, 0, 0, 5);
        gbc_textScanPath.fill = GridBagConstraints.HORIZONTAL;
        gbc_textScanPath.gridx = 0;
        gbc_textScanPath.gridy = 0;
        panel.add(textScanPath, gbc_textScanPath);
        textScanPath.setColumns(10);

        JButton btnBrowse = new JButton("Browse");
        btnBrowse.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser jFileChooser = new JFileChooser();
                jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                String text = textScanPath.getText();
                File dir = new File(text);
                if (dir.exists() && dir.isDirectory()) {
                    jFileChooser.setCurrentDirectory(dir);
                }
                int returnVal = jFileChooser.showOpenDialog(null);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = jFileChooser.getSelectedFile();
                    textScanPath.setText(selectedFile.getPath());
                }
            }
        });
        GridBagConstraints gbc_btnBrowse = new GridBagConstraints();
        gbc_btnBrowse.insets = new Insets(0, 0, 0, 5);
        gbc_btnBrowse.gridx = 1;
        gbc_btnBrowse.gridy = 0;
        panel.add(btnBrowse, gbc_btnBrowse);

        final JButton btnFindSuspects = new JButton("Find Suspects");
        GridBagConstraints gbc_btnFindSuspects = new GridBagConstraints();
        gbc_btnFindSuspects.insets = new Insets(0, 0, 0, 5);
        gbc_btnFindSuspects.gridx = 2;
        gbc_btnFindSuspects.gridy = 0;
        panel.add(btnFindSuspects, gbc_btnFindSuspects);

        JButton btnNewButton = new JButton("Clear");

        GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
        gbc_btnNewButton.insets = new Insets(0, 0, 0, 5);
        gbc_btnNewButton.gridx = 3;
        gbc_btnNewButton.gridy = 0;
        panel.add(btnNewButton, gbc_btnNewButton);

        final JCheckBox chckbxScrollLock = new JCheckBox("Scroll Lock");
        GridBagConstraints gbc_chckbxScrollLock = new GridBagConstraints();
        gbc_chckbxScrollLock.gridx = 4;
        gbc_chckbxScrollLock.gridy = 0;
        panel.add(chckbxScrollLock, gbc_chckbxScrollLock);

        JPanel panel_1 = new JPanel();
        GridBagConstraints gbc_panel_1 = new GridBagConstraints();
        gbc_panel_1.insets = new Insets(0, 0, 5, 0);
        gbc_panel_1.fill = GridBagConstraints.BOTH;
        gbc_panel_1.gridx = 0;
        gbc_panel_1.gridy = 1;
        contentPane.add(panel_1, gbc_panel_1);
        GridBagLayout gbl_panel_1 = new GridBagLayout();
        gbl_panel_1.columnWidths = new int[] { 0, 0 };
        gbl_panel_1.rowHeights = new int[] { 0, 0 };
        gbl_panel_1.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
        gbl_panel_1.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
        panel_1.setLayout(gbl_panel_1);

        txtCount = new JTextField();
        txtCount.setEditable(false);
        txtCount.setText("Count:");
        GridBagConstraints gbc_txtCount = new GridBagConstraints();
        gbc_txtCount.fill = GridBagConstraints.HORIZONTAL;
        gbc_txtCount.gridx = 0;
        gbc_txtCount.gridy = 0;
        panel_1.add(txtCount, gbc_txtCount);
        txtCount.setColumns(10);

        JPanel panel_2 = new JPanel();
        GridBagConstraints gbc_panel_2 = new GridBagConstraints();
        gbc_panel_2.fill = GridBagConstraints.BOTH;
        gbc_panel_2.gridx = 0;
        gbc_panel_2.gridy = 2;
        contentPane.add(panel_2, gbc_panel_2);
        GridBagLayout gbl_panel_2 = new GridBagLayout();
        gbl_panel_2.columnWidths = new int[] { 0, 0 };
        gbl_panel_2.rowHeights = new int[] { 0, 0 };
        gbl_panel_2.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
        gbl_panel_2.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
        panel_2.setLayout(gbl_panel_2);

        JScrollPane scrollPane = new JScrollPane();
        GridBagConstraints gbc_scrollPane = new GridBagConstraints();
        gbc_scrollPane.fill = GridBagConstraints.BOTH;
        gbc_scrollPane.gridx = 0;
        gbc_scrollPane.gridy = 0;
        panel_2.add(scrollPane, gbc_scrollPane);

        final JTextArea textArea = new JTextArea();
        scrollPane.setViewportView(textArea);
        btnFindSuspects.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btnFindSuspects.setEnabled(false);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        LeakSuspects s = new LeakSuspects();
                        s.setCountField(txtCount);
                        try {
                            s.workTree(textArea, chckbxScrollLock, textScanPath.getText());
                        }
                        catch (Exception e1) {
                            e1.printStackTrace();
                            Swings.appendText(textArea, chckbxScrollLock, "Exception!" + e1 + "\r\n", null);
                        }
                        finally {
                            btnFindSuspects.setEnabled(true);
                        }
                    }
                }).start();
            }
        });
        btnNewButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Swings.clearText(textArea);
            }
        });
    }
}
